package com.yisen.dasan.controller;

import com.alibaba.fastjson.JSONObject;
import com.yisen.dasan.config.Auth;
import com.yisen.dasan.pojo.Form;
import com.yisen.dasan.service.FormService;
import com.yisen.dasan.util.ResultData;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@CrossOrigin
@RequestMapping("/form")
public class FormController {
    @Autowired
    FormService formService;
    @Value("${file.uploadFolder}")
    private String uploadFolder;
    @GetMapping("/getForm")
    public ResultData getForm(@RequestParam("id") int id){
        Form form = formService.getForm(id);
        return new ResultData(true ,200,"成功！",form);
    }
    @PostMapping("/upload")
    public ResultData uploadImg(@RequestParam("file") MultipartFile file) throws IOException {
        // 获取文件名字
        String filename = file.getOriginalFilename();
        // 取得文件后缀
        String substring = filename.substring(filename.lastIndexOf("."));
        // 随机文件名
        String uuid = UUID.randomUUID().toString().replace("-", "");
        // 得到存储文件的名字
        String dest = uuid + substring;
        // 得到hashcode值并转16进制
        int hashCode = dest.hashCode();
        String hash = Integer.toHexString(hashCode);
        // 目标存储文件地址
//        String path = ClassUtils.getDefaultClassLoader().getResource("").getPath();
//        System.out.println(path);
        File file1 = new File(uploadFolder+ hash.charAt(0) + "/" + hash.charAt(1) + "/", dest);
        System.out.println(file1.toPath());
        file1.mkdirs();
        file.transferTo(file1);
        return new ResultData(true ,200,"成功！","/img/"+hash.charAt(0)+"/"+hash.charAt(1)+"/"+dest);
    }
    @DeleteMapping("/delform")
    @Auth(type = 1)
    public ResultData Del(@RequestParam("id") int formId){
        boolean flag=formService.delForm(formId);
        return new ResultData(true ,200,"成功！",flag);
    }
    @GetMapping("/getItem")
    @Auth(type = 1)
    public ResultData getItem(@RequestParam("formId") int formId,@RequestParam("type") String type){
        List<Map> list=formService.getItem(formId,type);
        return new ResultData(true,200,"成功",list);
    }
    @GetMapping("/getState")
    public ResultData getState(@RequestParam("formId") int formId){
        Map state=formService.getState(formId);
        return new ResultData(true,200,"成功",state);
    }
    @PostMapping("/state")
    public ResultData update(@RequestBody JSONObject jsonObject){
        boolean flag=formService.updateForm(jsonObject);
        return new ResultData(true,200,"成功",flag);
    }
    @PostMapping("/update")
    public ResultData updateFormNews(@RequestBody JSONObject jsonObject){
        boolean flag=formService.updateFormNews(jsonObject);
        return new ResultData(true,200,"成功",flag);
    }
}
